package Sagittarius.Manager
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import com.pblabs.engine.debug.Logger;
	
	/**************************************************************************
	 * ...
	 * @author Aqua
	 *************************************************************************/
	
	public class ResourceManager
	{
		
		//---------------------//
		//      Singleton      //
		//---------------------//
		
		/**************************************************************************
		 * function					instance getter
		 * description				a private function used to enforce singlton
		 * @return	ResourceManager	singleton instance of this class
		 *************************************************************************/
		public static function get instance():ResourceManager
		{
			if(null == _instance)
			{
				_instance = new ResourceManager(singleton);
			}
		
			return _instance;
		}
		
		/**************************************************************************
		 * function				singleton
		 * description			a private function used to enforce singlton
		 *************************************************************************/
		private static function singleton():void
		{
			
		}
		
		
		//---------------------//
		//     Constructor     //
		//---------------------//
		
		/**************************************************************************
		 * function				ResourceManager
		 * description			constructor
		 * @param	s			a private function used to enforce singlton
		 *************************************************************************/
		public function ResourceManager(s:Function = null):void
		{
			if (singleton != s)
			{
				throw new Error("This is a singleton class!");
			}
			
			_bulkLoader = new BulkLoader("");
			_loadStarted = false;
		}
		
		
		//-----------------//
		//     Publics     //
		//-----------------//
		
		/**************************************************************************
		 * function				addResource
		 * description			add a file to be loaded
		 * @param	url			url of the file
		 * @param	id			the unique id of this resource
		 *************************************************************************/
		public function addResource(url:String, id:String):void
		{
			if (_loadStarted)
			{
				Logger.print(this, "File can't be added after loading starts!");
				return;
			}
			
			_bulkLoader.add(url, {id:id});
		}
		
		/**************************************************************************
		 * function				getResource
		 * description			get a resource that has been loaded
		 * @param	id			the unique id of this resource
		 * @return	*			required resource
		 *************************************************************************/
		public function getResource(id:String):*
		{
			return _bulkLoader.get(id);
		}
		
		/**************************************************************************
		 * function				startUp
		 * description			start loading files
		 * @param	callback	a function which would be called when all files 
		 * 						loaded
		 *************************************************************************/
		public function startUp(callback:Function = null):void
		{
			_onLoadComplete = callback;
			_loadStarted = true;
			
			_bulkLoader.addEventListener(BulkLoader.COMPLETE, onComplete);
			_bulkLoader.start();
			
			function onComplete(e:BulkProgressEvent):void
			{
				if (null != _onLoadComplete)
				{
					_onLoadComplete();
				}
			}
		}
		
		
		private static var _instance:ResourceManager;
		
		private var _bulkLoader		:BulkLoader;
		private var _loadStarted	:Boolean;
		private var _onLoadComplete	:Function;
		private var _resourcePool	:Object;
		
	}
}